package com.wits.av;

import android.util.Log;

import java.io.Closeable;
import java.io.IOException;

/**
 * Created by zhengboyuan on 2018-08-27.
 */

public class RecordStore implements Closeable {

    protected int handle = JniRecordStore.INVALID_HANDLE;

    public RecordStore() {

    }


    @Override
    protected void finalize() {
        try {
            close();
        } catch (Throwable throwable) {
            //
        }
    }

    public boolean open(String filepath) {
        try {
            close();
        } catch (Throwable throwable) {
            //
        }

        handle = JniRecordStore.recordstore_open(filepath);
        return isOpen();
    }

    @Override
    public void close() throws IOException {
        if (handle != JniRecordStore.INVALID_HANDLE) {
            JniRecordStore.recordstore_close(handle);
            handle = JniRecordStore.INVALID_HANDLE;
        }
    }

    public boolean isOpen() {
        return (handle != JniRecordStore.INVALID_HANDLE);
    }

    public int countRecord(MRecordQuery query) {
        return JniRecordStore.recordstore_countRecord(query);
    }

    public int findRecord(MRecordQuery query, int offset, MRecord[] records) {
        return JniRecordStore.recordstore_findRecord(handle, query, offset, records);
    }

    public boolean deleteRecord(int id) {
        return JniRecordStore.recordstore_deleteRecord(handle, id);
    }


    public void clearBrokenRecord() {
        JniRecordStore.recordstore_clearBrokenRecord(handle);
    }


    
}
